//
//  APPLEMainTableBarViewController.swift
//  123
//
//  Created by pangjiaxing on 15/12/27.
//  Copyright © 2015年 pangjiaxing. All rights reserved.
//

import UIKit

class APPLEMainTableBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabbar=APPLETabBar()
        let vcbag={[weak self] in
            print("点击了加号按钮的回调闭包，我在APPLEMainTableBarViewController中")
            if self !== nil{
                let  appleComposeview:APPLEComposeView=APPLEComposeView()
                appleComposeview.show(self!)
            }
          
        }
        
        tabbar.plusButtonBag=vcbag
        
        setValue(tabbar, forKeyPath: "tabBar")
        
       addChildViewController()
    }

    //tabbar子控制器的添加
   private func  addChildViewController() {
        addChildViewController(APPLEHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(APPLEMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(APPLEDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        
        
        addChildViewController(APPLEProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    //tabbar添加子控制器的方法提取：其中包括参数1.控制器的，和2.控制器的名称和3.图片是一个提取的代码块
  
    func addChildViewController(vc: UIViewController,imageName:String,title:String) {
        
        vc.tabBarItem=APPLETabBarItem()
        //用于设置当前控制器的title
        vc.title=title
        //?为可选值，如果没有值，则赋值，不显示图片，其它同理
        
        vc.tabBarItem.image=UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        vc.tabBarItem.selectedImage=UIImage(named:"\(imageName)_selected" )?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let font=[NSForegroundColorAttributeName:UIColor.orangeColor()]
        vc.tabBarItem.setTitleTextAttributes(font, forState: UIControlState.Selected)
        //此处是一个被navigation包装的vc控制器，然后添加到tabbarcontroller上面
        //使用自己的导航控制器包装出来的然后不断的推送控制器（APPLETempViewController）
        addChildViewController(APPLENavigationViewController(rootViewController: vc))
    }
}
